import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getUserOrderAPI } from '@/apis/order'

export const useOrderStore = defineStore(
  'order',
  () => {
    // 获取token
    // 定义state
    const orderInfo = ref({})
    const total = ref(0)

    // 获取用户地址和购物车商品信息
    const getOrderInfo = async (params) => {
      try {
        const res = await getUserOrderAPI(params.value) // 如果 params 不是 ref，直接传入 params
        orderInfo.value = res.rows
        total.value = res.total
      } catch (error) {
        console.error('获取订单信息失败:', error)
      }
    }

    //清空
    const clearOrder = () => {
      orderInfo.value = {}
      total.value = 0
    }

    // 返回
    return { orderInfo, getOrderInfo, total, clearOrder }
  },
  { persist: true }
)
